package foodserver;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.subway.babynote.syncdata.*;


import querysrc.query_authenticate;
import querysrc.query;
import querysrc.sqlConnectionHandler;
import querysrc.sqlConnectionHandlerManager;
import serverdata.httpreqhandler;
/**
 * Servlet implementation class getbase
 */

public abstract class getbase extends HttpServlet {
	protected static final long serialVersionUID = 1L;
	protected sqlConnectionHandler sqlconhandler = null;	
	protected query_authenticate qau = null;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public getbase() {
        super();
        // TODO Auto-generated constructor stub
    }

    public void init()throws ServletException 
	{
	    try {
	    	sqlconhandler = sqlConnectionHandlerManager.getHandler();	    		 
	    	qau = new query_authenticate(this.sqlconhandler);
	    	this.initialize_subquery();
	      }
	      catch (Exception se) {
	        System.out.println("Exception: "+se.getMessage());
	        se.printStackTrace();
	      }
	}
	protected abstract void initialize_subquery();
	protected abstract void destory_subquery();
	
	protected void release_query(query q)
	{
		if(q != null)
		{
			q.finalize();
			q = null;
		}
	}
	
	public void destroy () {
		    try {
		    	sqlconhandler = null;
		    	this.release_query(qau);
		    	this.destory_subquery();
		    }     
		    catch (Exception e) {
		      e.printStackTrace();
		    }      
	
	 }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 try
		 {		 
			 int iret = 0;
			 JSONObject jsono = httpreqhandler.readInput(request);
			 ClientDataRecord.user_auth_info uai = new ClientDataRecord.user_auth_info(jsono);
			 JSONArray jsona = new JSONArray();
			 if(qau.sync_authen(uai.id, uai.imsi, uai.imei))
			 {				 
				 List<ClientDataRecordBase> al= this.get_data(uai);
				 Iterator<ClientDataRecordBase> it = al.iterator();
				 while (it.hasNext())
				 {
					 ClientDataRecordBase cdrb = it.next();
					 JSONObject tempjsono =  cdrb.toJSON();
					 jsona.put(tempjsono);
				 }				 
			 }
			 else
			 {
				 iret = errMessage.err_authenfail.errcode;
			 }
			 
			 JSONObject return_value = new JSONObject();
			 return_value.put(PostParam.ReturnValue_Param.return_status, iret);
			 return_value.put(PostParam.last_avgRecord_Param.records, jsona);
			 //return_value.put(PostParam.ReturnValue_Param.err_message, errMessage.err_authenfail.errcode);
			 httpreqhandler.writeOutput(return_value, response);		     			 
		 }
		 catch (JSONException e) {
				// TODO Auto-generated catch block
				System.out.println("JSON error for FeedSync doPost:" + e.getMessage());
				e.printStackTrace();
				
			}
		 catch (Exception e) {
			 System.out.println("FeedSync doPost error:" + e.getMessage());
		      e.printStackTrace();
		    }
	}
	
	protected abstract List<ClientDataRecordBase> get_data(ClientDataRecord.user_auth_info uai);
	

}
